FunctionIndex
Index = FunctionIndex(FunctionName$)
 
Parameters:

    FunctionName$ = Name of the function
Returns:

    Index = The Index of this function
 

     The FunctionIndex function allows us calculate a functions internal index. We can then use this to call the function with the CallFunction command.

      The benefit of pre-calculating the functions index, rather than calling it by name all the time. Is that this bypasses the need for the CallFunction operation to constantly resolve the functions index from the name we provide it. Now If you're only calling a function a few times, then it probably doesn't matter too much, but if you're going to be calling it a lot, then this overhead really adds up.




FACTS:



* FunctionIndex can only pass Integer,Float and String variables in PlayBASIC V1.64k

* FunctionIndex can't currently handle return values in PlayBASIC V1.64k




Mini Tutorial #1:


      This example sets up an array that will contain our list of function indexes. Within this array, we're storing the FunctionIndex function of each function we wish to call. Then at run time, we can pass these indexes to CallFunction to execute the function on demand.


  
  Dim MyFunctions(1)
  
; Convert the function names we want to call
; into their internal function indexes
  MyFunctions(0)=FunctionIndex("Yeah")
  MyFunctions(1)=FunctionIndex("SubYeah")
  
  Do
     
   ; clear the screen
     Cls
     
   ; Run through the call functions
     For lp=0 To 1
        CallFunction MyFunctions(lp),Rnd(100),Rnd#(200)
     Next
     
     Sync
  Loop
  
  
  
  
  
Function  Yeah(x,y#)
  Ink $ff0000
  Print "Running Function"
  Print x
  Print y#
  Ink $ffffff
EndFunction
  
  
  
Psub  SubYeah(x,y#)
  Ink $00ff00
  Print "Running Psub"
  Print x
  Print y#
  Ink $ffffff
EndPsub
  





Mini Tutorial #2:


      This example compares how long it takes to call a function by name or by index using CallFunction


  
  
  
  Do
     
   ; clear the screen
     Cls
     
     Frames++
     
   ; call the Test function 5000 time
   ;and time how long it takes
     
     t=Timer()
     For lp=0 To 5000
      ; Call the function by name
        CallFunction "Test"
     Next
     tt1#+=Timer()-t
     Print "Calling FUnction By Name:"+Str$(tt1#/frames)
     
     
     t=Timer()
     Index=FunctionIndex("Test")
     For lp=0 To 5000
      ; Call the function by Index this time
        CallFunction Index
     Next
     tt2#+=Timer()-t
     Print "Calling FUnction By Name:"+Str$(tt2#/frames)
     
     
     Sync
  Loop
  
  
  
Psub  Test()
EndPsub
  
  




 
Related Info: CallFunction | Function | FunctionExist | Functions&Psub | Psub :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com